package com.iims.web.controller.monitor;

import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.common.utils.poi.ExcelUtil;
import com.iims.system.domain.SysLogininfo;
import com.iims.system.service.ISysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author fanpei
 */
@Api(tags = "C-日志管理")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Autowired
    private ISysLogininforService logininforService;

    @ApiOperation("分页查询登录日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query",required=true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query",required=true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "登录地址", name = "ipaddr", paramType = "query"),
            @ApiImplicitParam(value = "用户名称", name = "userName", paramType = "query"),
            @ApiImplicitParam(value = "状态:值来源于字典sys_common_status", name = "status", paramType = "query"),
            @ApiImplicitParam(value = "开始登录时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "结束登录时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore SysLogininfo logininfor) throws Exception {
        setSearchTimeRange(logininfor);
        startPage();
        List<SysLogininfo> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @GetMapping("/export")
    public AjaxResult export(SysLogininfo logininfor) {
        List<SysLogininfo> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfo> util = new ExcelUtil<SysLogininfo>(SysLogininfo.class);
        return util.exportExcel(list, "登录日志");
    }

    @ApiOperation("登录日志清除")
    @ApiImplicitParam(name = "infoIds", value = "登录日志id列表，逗号分隔", required = true, dataType = "List")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    @ApiOperation("登录日志清空")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        logininforService.cleanLogininfor();
        return AjaxResult.success();
    }
}
